package huu.bio;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author huu
 * @create 2025-08-26-23:18
 */
public class BioClient {
    public static void main(String[] args) throws InterruptedException {
        Thread tom = new Thread(() -> {
            try {
                sendHello();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, "tom");
        Thread jerry = new Thread(() -> {
            try {
                sendHello();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }, "jerry");
        tom.start();
        jerry.start();
        //当前线程调用线程的join方法，会阻塞当前线程，直到调用线程执行完毕
        tom.join();
        jerry.join();


    }
    public static void sendHello() throws IOException{
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("localhost", 8080));
        OutputStream outputStream = socket.getOutputStream();
        for(int i = 0; i < 10; i++){
            outputStream.write(("hello" + Thread.currentThread().getName()).getBytes());
            outputStream.flush();
        }
        socket.close();
    }
}
